package day2_linkedlist.mylink;

/**
 * @Author: yuisama
 * @Date: 2021/8/28 17:06
 * @Description:自己实现单链表
 */
public class LinkedList<E> {
    private class Node {
        public E e;
        public Node next;

        public Node() {}
        public Node(E e) {
            this.e = e;
        }

        public Node(E e,Node next) {
            this.e = e;
            this.next = next;
        }
    }
    // 虚拟头结点
    private Node dummyHead = new Node();
    // 链表长度
    private int size;

    public int getSize() {
        return size;
    }

    // 判断索引合法性
    private boolean isIndexLegal(int index) {
        if (index < 0 || index > size) {
            return false;
        }
        return true;
    }

    // 在链表头部添加元素
    public void addFirst(E e) {
        add(e,0);
    }

    public void addLast(E e) {
        add(e,size);
    }

    // 在链表任意位置添加元素
    public void add(E e,int index) {
        if (isIndexLegal(index)) {
            // 找到索引位置的前驱节点
            Node prev = dummyHead;
            for (int i = 0; i < index; i++) {
                prev = prev.next;
            }
            prev.next = new Node(e,prev.next);
            size ++;
        }
    }

    // 判断元素e是否在链表中
    public boolean contains(E e) {
        Node cur = dummyHead.next;
        while (cur != null) {
            if (cur.e.equals(e)) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    // 返回链表中索引为index的节点值
    public E get(int index) {
        if (isIndexLegal(index)) {
            Node cur = dummyHead.next;
            for (int i = 0; i < index; i++) {
                cur = cur.next;
            }
            return cur.e;
        }
        throw new IllegalArgumentException("索引非法");
    }

    // 修改链表中索引为index的节点值
    public boolean set(int index,E newValue) {
        if (isIndexLegal(index)) {
            Node cur = dummyHead.next;
            for (int i = 0; i < index; i++) {
                cur = cur.next;
            }
            cur.e = newValue;
            return true;
        }
        return false;
    }

    // 删除链表中索引为index的节点
    public E removeNode(int index) {
        Node prev = dummyHead;
        for (int i = 0; i < index; i++) {
            prev = prev.next;
        }
        Node cur = prev.next;
        prev.next = cur.next;
        cur.next = null;
        size --;
        return cur.e;
    }

    // 删除链表中所有节点值为e的节点
    public void removeValue(E e) {
        Node prev = dummyHead;
        while (prev.next != null) {
            if (prev.next.e.equals(e)) {
                prev.next = prev.next.next;
            }else {
                prev = prev.next;
            }
        }
    }


    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        Node node = dummyHead.next;
        while (node != null) {
            sb.append(node.e).append("->");
            if (node.next == null) {
                sb.append("NULL");
            }
            node = node.next;
        }
        return sb.toString();
    }
}
